//
//  DeployableLabel.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/21.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "DeployableLabel.h"

static CGFloat const FromTheRight = 14 + 8;

@interface DeployableLabel ()
@property(nonatomic,strong)UILabel *textLabel;
@property(nonatomic,strong)UIButton *btn;
@end

@implementation DeployableLabel

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)setObject:(id<Deployable>)object
{
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:@"问题描述: " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:object.content attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:k666666}]];
    self.textLabel.attributedText = attString;
    self.btn.selected = object.spread;
    if (object.spread) {
        self.textLabel.numberOfLines = 0;
    }else{
        self.textLabel.numberOfLines = 1;
    }
    _object = object;
    _object.content_H = [self.textLabel.attributedText.string heightForFontSize:14 andWidth:self.width - FromTheRight];
}

-(void)initView
{
    [self addSubview:self.textLabel];
    [self addSubview:self.btn];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.trailing.equalTo(self).with.offset(-FromTheRight);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);
        make.width.equalTo(@(FromTheRight));
        make.height.equalTo(@(FromTheRight));
        make.bottom.equalTo(self);
    }];
}

-(void)spreadTextAction:(UIButton *)sender
{
    self.object.spread = !self.object.spread;
    if (self.spreadText) {
        self.spreadText(self);
    }
}

-(UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]init];
    }
    return _textLabel;
}
-(UIButton *)btn
{
    if (!_btn) {
        _btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btn setImage:[UIImage imageNamed:@"StartIcon"] forState:(UIControlStateNormal)];
        [_btn setImage:[UIImage imageNamed:@"PackUpIcon"] forState:(UIControlStateSelected)];
        [_btn addTarget:self action:@selector(spreadTextAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _btn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
